package edu.maping.easy.painter.plugins;

import java.util.Set;

import processing.core.PApplet;
import controlP5.ControlEvent;
import controlP5.ControlGroup;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.Util;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.model.Point;
import edu.maping.easy.model.PolygonThing;
import edu.maping.easy.model.Thing;
import edu.maping.easy.painter.EventPainter;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.UtilPainter;
import edu.maping.easy.sequence.ActionConf;

public class ThingPainterContournTransform implements ThingPainterDelegate {

	private int indexStep = 0;

	private static int STEPS = 200;

	private static int SIZE_TAIL = 20;

	int blue = 0;
	int green = 0;
	int red = 0;

	private UtilPainter utilPainter = new UtilPainter();

	public void draw(Thing cosa, PApplet pApplet, Object object) {

		pApplet.pushStyle();

		indexStep++;
		if (indexStep > STEPS) {
			pApplet.fill(pApplet.random(255), pApplet.random(255), pApplet
					.random(255));
			// pApplet.stroke(255, 255, 255);
			pApplet.noStroke();
			indexStep = 0;
		}

		PolygonThing poly = (PolygonThing) cosa;

		float x = 0;

		Point first = null;

		int endIndex = poly.vertex.size();

		if (poly.vertex.size() < 4) {
			endIndex -= 1;
		}

		if (poly != null && poly.vertex != null)
			for (int index = 0; index < endIndex; index++) {

				Point vertex = poly.vertex.get(index);

				if (first == null) {
					first = vertex;
					continue;
				} else {

					pApplet.pushMatrix();

					pApplet.translate(first.x, first.y);

					pApplet.fill(red, green, blue);
					pApplet.stroke(red, green, blue);

					float angle = PApplet.atan2(vertex.x - first.x, vertex.y
							- first.y);
					pApplet.rotate(-angle + PApplet.HALF_PI);

					float dist = (float) Util.getDistance(vertex, first);
					float step = dist / (float) STEPS;

					float xStart = indexStep - SIZE_TAIL;

					if (xStart < 0) {
						xStart += STEPS;
						xStart *= step;
						// for (; xStart < dist; xStart++) {
						// pApplet.ellipse(xStart, 0, 5, 5);
						// }
						pApplet.ellipse(xStart, 2, 5, 5);
						pApplet.rect(xStart, 5, dist, 0);
						pApplet.ellipse(dist, 2, 5, 5);
						xStart = 0;
					}

					xStart *= step;

					float xEnd = indexStep * step;

					// for (; xStart < xEnd; xStart++) {
					// pApplet.ellipse(xStart, 0, 5, 5);
					// }
					pApplet.ellipse(xStart, 2, 5, 5);
					pApplet.rect(xStart, 5, xEnd, 0);
					pApplet.ellipse(xEnd, 2, 5, 5);

					pApplet.popMatrix();
					first = vertex;
				}

			}
		pApplet.popStyle();
	}

	public void draw(Thing body, PApplet pApplet) {
		draw(body, pApplet, null);
	}

	public void setup() {
	}

	@Override
	public void setConf(ActionConf actionConf) {

		// utilPainter.reset();

		for (int i = 0; i < utilPainter.size(); i++) {
			Slider slider = (Slider)utilPainter.get(i);
			String key = slider.name;
			if (actionConf.containsKey(key)) {
				float value = actionConf.get(key);
				setParameters(key, value);
				utilPainter.setParameter(key, value);
			} else {
				utilPainter.setParameter(key, 0);
			}
		}

	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "contournWorm";
	}

	@Override
	public EventPainter controlEvent(GUIEvent theEvent) {
		EventPainter eventPainter = utilPainter.controlEvent(theEvent);
		if (eventPainter == null) {
			System.out.println("Error al controlar el evento:"
					+ theEvent.name());
		} else {
			setParameters(eventPainter.name, eventPainter.value);
		}
		return eventPainter;
	}

	private void setParameters(String name, float value) {

		if (name.equals("VELOCIDAD")) {
			STEPS = (int) value;
		} else if (name.equals("SIZE")) {
			SIZE_TAIL = (int) value;
		} else if (name.equals("RED")) {
			red = (int) value;
		} else if (name.equals("GREEN")) {
			green = (int) value;
		} else if (name.equals("BLUE")) {
			blue = (int) value;
		}
	}

	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {

		utilPainter.setup(pApplet, controlFacade, buttonList, guiFactory);
		utilPainter.add("VELOCIDAD", 0, 255);
		utilPainter.add("SIZE", 0, 255);
		utilPainter.add("RED", 0, 255);
		utilPainter.add("GREEN", 0, 255);
		utilPainter.add("BLUE", 0, 255);

	}

	@Override
	public void showForm(int x, int y) {
		utilPainter.setPosition(x + 100, y);
		utilPainter.render();
	}

	@Override
	public void hideForm() {
		utilPainter.hide();
	}

	public ActionConf createNewActionConf() {
		return utilPainter.createActionConf();
	}

	@Override
	public ThingPainterDelegate createNewInstance() {
		ThingPainterContournTransform contournTransform = new ThingPainterContournTransform();
		contournTransform.utilPainter = utilPainter.clone();
		return contournTransform;
	}
}
